package com.songlanyun.modules.schedule;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.config.dao.YxtUserTeamRateConfigDao;
import com.songlanyun.modules.config.entity.YxtUserTeamRateConfig;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.pointconfig.entity.PointConfig;
import com.songlanyun.modules.pointconfig.service.PointConfigService;
import com.songlanyun.modules.usergains.service.UserBillService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
@Configuration
@EnableScheduling
@Lazy(false)
@Slf4j
public class MonthlyDirectorDividendsScheduleImpl {

    @Autowired
    private UserBillService userBillService;
    @Autowired
    private PointConfigService pointConfigService;

    @Autowired
    private UserIdentityAssetService userIdentityAssetService;

    @Autowired
    private YxtUserTeamRateConfigDao yxtUserTeamRateConfigDao;

    @Autowired
    private AccountService accountEntityService;

    @Autowired
    private OrderService orderService;



    /**
     *
     */
    @Async
    @Scheduled(cron = "${taskmonthOh}")
//    @Scheduled(cron = "${task30s}")
    //@Scheduled(cron = "${task05h}")
    public void monthlyDirectorDividends() {


        List<YxtUserTeamRateConfig> yxtUserTeamRateConfigs = yxtUserTeamRateConfigDao.selectList(new LambdaQueryWrapper<>());
        List<YxtUserTeamRateConfig> returnList = new ArrayList<>(Collections.nCopies(yxtUserTeamRateConfigs.size()+1, null));
        for(int i = 0; i<yxtUserTeamRateConfigs.size(); i++){
            returnList.set(Math.toIntExact(yxtUserTeamRateConfigs.get(i).getId()), yxtUserTeamRateConfigs.get(i));
        }
        if (ObjectUtil.isNotNull(yxtUserTeamRateConfigs)) {

            //查询符合条件的董事
            List<AccountEntity> accountEntityList = accountEntityService.list(new LambdaQueryWrapper<AccountEntity>().gt(AccountEntity::getDirectorGrade, 0));
            //查询每个月的贡献值

            for (AccountEntity accountEntity : accountEntityList) {
                // 获取当前日期
                LocalDate currentDate = LocalDate.now();
                // 获取上个月的第一天
                LocalDate firstDayOfLastMonth = currentDate.minusMonths(1).withDayOfMonth(1);
                // 获取上个月的最后一天
                LocalDate lastDayOfLastMonth = currentDate.minusMonths(1).withDayOfMonth(currentDate.minusMonths(1).lengthOfMonth());
                // 查询上个月的总消费额
                BigDecimal totalAmount = orderService.getCountTotalAmount(firstDayOfLastMonth, lastDayOfLastMonth);
                //按比例分配
                BigDecimal personalProperty = totalAmount.multiply(returnList.get(accountEntity.getDirectorGrade()).getManagerWithdrawRate()).setScale(2, RoundingMode.HALF_UP);
                userBillService.monthlyDirectorDividends(accountEntity.getId(), personalProperty);
            }

        }

    }
}


